package com.pctx.bibaregisterlogin.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.MD5Util;
import com.pctx.bibaregisterlogin.dao.CompanyMapper;
import com.pctx.bibaregisterlogin.dao.DepartmentMapper;
import com.pctx.bibaregisterlogin.dao.PositionMapper;
import com.pctx.bibaregisterlogin.dao.UserMapper;
import com.pctx.bibaregisterlogin.pageHelper.PageInfo;
import com.pctx.bibaregisterlogin.pojo.Company;
import com.pctx.bibaregisterlogin.pojo.Department;
import com.pctx.bibaregisterlogin.pojo.Position;
import com.pctx.bibaregisterlogin.pojo.User;
import com.pctx.bibaregisterlogin.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Result addDepartment(String departmentname, String username) {
        User user = userMapper.selectByUsername(username);
        Department department = new Department();
        UUID uuid = UUID.randomUUID();
        department.setUuid(uuid.toString());
        department.setDepartmentName(departmentname);
        department.setCompanyUuid(user.getCompanyUuid());
        Date date = new Date();
        //Timestamp nowdate = new Timestamp(date.getTime());
        department.setCreateTime(date);
        department.setUpdateTime(date);
        Boolean flag = departmentMapper.insert(department) > 0;
        if (flag) {
            return Result.success(department, "部门建立成功");
        } else {
            return Result.error("部门建立失败");
        }

    }

   /* @Override
    public Result addPosition(String positionname, String departmentname, String username) {
        User user = userMapper.selectByUsername(username);
        Department department = departmentMapper.selectByname(departmentname);
        Position position = new Position();
        position.setCompanyUuid(user.getCompanyUuid());
        UUID uuid = UUID.randomUUID();
        position.setUuid(uuid.toString());
        position.setPositionName(positionname);
        position.setDepartmentUuid(department.getUuid());
        Date date = new Date();
        position.setCreateTime(date);
        position.setUpdateTime(date);
        Boolean flag = positionMapper.insert(position) > 0;
        if (flag) {
            return Result.success(null, "职位建立成功");
        } else {
            return Result.error("职位建立失败");
        }
    }*/

    @Override
    public Result addPositionone(String positionname, String departmentuuid, String username) {
        //User user = userMapper.selectByUsername(username);
        Department department = departmentMapper.selectByUuid(departmentuuid);
        Position position = new Position();
        position.setCompanyUuid(department.getCompanyUuid());
        UUID uuid = UUID.randomUUID();
        position.setUuid(uuid.toString());
        position.setPositionName(positionname);
        position.setDepartmentUuid(departmentuuid);
        Date date = new Date();
        position.setCreateTime(date);
        position.setUpdateTime(date);
        Boolean flag = positionMapper.insert(position) > 0;
        if (flag) {
            return Result.success(position, "职位建立成功");
        } else {
            return Result.error("职位建立失败");
        }
    }

    @Override
    public Result selectDepartment(String companyUuid) {
        List<Department> list = departmentMapper.selectByCompanyuuid(companyUuid);
        return Result.success(list);
    }


    @Override
    public Result selectPosition(String companyUuid, String departmentuuid) {
        List<Position> list = positionMapper.selectByDepartmentuuid(departmentuuid);

        return Result.success(list);
    }

   /* @Override
    public Result addUser(String username, String name, String mobile, String mail, String sex, String departmentname, String positionname, String upperleader, String subordinate, String loginuser) {
        User userone = userMapper.selectByUsername(loginuser);
        UUID uuid = UUID.randomUUID();
        User user  = new User();
        user.setUuid(uuid.toString());
        user.setUsername(username);
        user.setName(name);
        user.setPassword(MD5Util.getMD5("666666"));
        user.setRealpassword("666666");
        user.setMail(mail);
        user.setMobile(mobile);
        if(sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByname(departmentname);
        user.setDepartmentUuid(department.getUuid());
        Position position=positionMapper.selectByname(positionname);
        user.setPositionUuid(position.getUuid());
        if(subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
         user.setCompanyUuid(userone.getCompanyUuid());
         User usertwo=userMapper.selectByUsername(upperleader);
         if(usertwo.getDepartmentUuid().equals(department.getUuid()))
         user.setUpperUuid(usertwo.getUuid());
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        return Result.success(null, "用户创建成功");
    }*/

    @Override
    public Result addUser(String username, String name, String mobile, String mail, String sex, String departmentuuid, String positionuuid, String upperUuid, String subordinate) {
        // User userone = userMapper.selectByUsername(loginuser);
        UUID uuid = UUID.randomUUID();
        User user = new User();
        user.setUuid(uuid.toString());
        user.setUsername(username);
        user.setName(name);
        user.setPassword(MD5Util.getMD5("666666"));
        user.setRealpassword("666666");
        user.setMail(mail);
        user.setMobile(mobile);
        if (sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByUuid(departmentuuid);
        user.setDepartmentUuid(departmentuuid);
        // Position position=positionMapper.selectByname(positionname);
        user.setPositionUuid(positionuuid);
        if (subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
        user.setCompanyUuid(department.getCompanyUuid());
//        User usertwo=userMapper.selectByUsername(upperleader);
//        if(usertwo.getDepartmentUuid().equals(departmentuuid))
//            user.setUpperUuid(usertwo.getUuid());
        user.setUpperUuid(upperUuid);
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        Boolean flag = userMapper.insert(user) > 0;
        if (flag)
            return Result.success(user, "用户创建成功");
        else
            return Result.error("用户创建失败");
    }


    @Override
    public Result deleteUser(String username) {
        Boolean flag = userMapper.deleteByname(username) > 0;
        if (flag)
            return Result.success(null, "成功删除用户");
        else
            return Result.error("删除失败");

    }

    @Override
    public Result updateUser(String username, String name, String mobile, String mail, String sex, String departmentuuid, String positionuuid, String upperleaderuuid, String subordinate) {
        User user = userMapper.selectByUsername(username);
        // user.setUsername(username);
        user.setName(name);
        user.setMobile(mobile);
        user.setMail(mail);
        if (sex.equals("男"))
            user.setSex(1);
        else
            user.setSex(2);
        Department department = departmentMapper.selectByUuid(departmentuuid);
        user.setDepartmentUuid(department.getUuid());
        Position position = positionMapper.selectByUuid(positionuuid);
        user.setPositionUuid(position.getUuid());
        //User usertwo=userMapper.selectByUpperleaderuuid(upperleaderuuid);
        user.setUpperUuid(upperleaderuuid);
        if (subordinate.equals("无"))
            user.setSubordinate(0);
        else
            user.setSubordinate(1);
        Date date = new Date();
        user.setUpdateTime(date);
        Boolean flag = userMapper.update(user) > 0;
        if (flag)
            return Result.success(user, "修改成功");
        else
            return Result.error("修改失败");

    }

    @Override
    public Result resetPassword(String username) {
        User user = userMapper.selectByUsername(username);
        user.setRealpassword("666666");
        user.setPassword(MD5Util.getMD5("666666"));
        Date date = new Date();
        user.setUpdateTime(date);
        Boolean flag = userMapper.updateByusername(user) > 0;
        if (flag)
            return Result.success(user, "密码重置成功");
        else
            return Result.error("密码重置失败");
    }

    @Override
    public Result hassubordinate(String companyUuid) {
        List<User> list = userMapper.selectHasSubordinate(companyUuid);
        return Result.success(list);
    }

    @Override
    public Result personalMessage(String username) throws Exception {

        User user = userMapper.selectByUsername(username);
        Company company = companyMapper.selectByUuid(user.getCompanyUuid());
        Department department = departmentMapper.selectByUuid(user.getDepartmentUuid());
        Position position = positionMapper.selectByUuid(user.getPositionUuid());
        user.setDepartment(department);
        user.setCompany(company);
        user.setPosition(position);
        return Result.success(user, "成功");
    }

    @Override
    public Result getUser4Company(String companyUuid, String username, Date startTime, Date endTime, String
            departmentUuid, String upperUuid) throws Exception {
        if (StringUtils.isEmpty(companyUuid))
            return null;

        List<Map> list = userMapper.get4Company(companyUuid, username, startTime, endTime, departmentUuid,upperUuid);

        return Result.success(new PageInfo(list));
    }

    @Override
    public Result getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return Result.error("参数不能为空");

        User user = userMapper.getByUuid(uuid);
        return Result.success(user);
    }


}